<!--********************************************************************
* Copyright© 2000 - 2025 SuperMap Software Co.Ltd. All rights reserved.
*********************************************************************-->
<!--********************************************************************
* 该示例需要引入
* UGCWasm (https://iclient.supermap.io/web/libs/ugcwasm/1.0.1/UGCWasmAll.js)
*********************************************************************-->
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title data-i18n="resources.title_ugcSpatial"></title>
    <style>
      body {
        margin: 0;
        overflow: hidden;
        background: #fff;
        width: 100%;
        height: 100%;
        position: absolute;
        top: 0;
      }
      #toolbar {
        position: absolute;
        top: 50px;
        right: 10px;
        width: 280px;
        text-align: center;
        z-index: 100;
        border-radius: 4px;
      }
    </style>
  </head>

  <body>
    <div id="toolbar" class="panel panel-primary">
      <div class="panel-heading">
        <h5 class="panel-title text-center">空间关系</h5>
      </div>
      <div class="panel-body content">
        <div class="panel">
          <div class="input-group">
            <span class="input-group-addon">查找对象</span>
            <select id="positon" class="form-control">
              <option value="hasTouch">边界相触</option>
            </select>
          </div>
        </div>
        <input type="button" class="btn btn-default" value="查找" onclick="search()" />&nbsp;
        <input type="button" class="btn btn-default" value="移除" onclick="clearLayer()" />
      </div>
    </div>
    <div id="map" style="width: 100%; height: 100%"></div>
    <script type="text/javascript" include="bootstrap-css" src="../js/include-web.js"></script>
    <script type="text/javascript" include="ugcwasm" src="../../dist/maplibregl/include-maplibregl.js"></script>
    <script>
      var map, targetPolygon, searchPolygons, searchLines, searchPoints;
      var host = window.isLocal ? window.server : 'https://iserver.supermap.io';
      var url = host + '/iserver/services/map-world/rest/maps/World/zxyTileImage.png?z={z}&x={x}&y={y}';
      function initMap(url) {
        return new Promise((resolve) => {
          map = new maplibregl.Map({
            container: 'map',
            style: {
              version: 8,
              sources: {
                'raster-tiles': {
                  type: 'raster',
                  tiles: [url],
                  tileSize: 256
                }
              },
              layers: [
                {
                  id: 'simple-tiles',
                  type: 'raster',
                  source: 'raster-tiles'
                }
              ]
            },
            center: [120, 30],
            zoom: 6
          });
          map.on('load', function () {
            resolve(map);
          });
        });
      }

      function addFeatures() {
        targetPolygon = {
          type: 'Feature',
          properties: {},
          geometry: {
            coordinates: [
              [
                [120, 34],
                [115, 34],
                [115, 30],
                [120, 30],
                [120, 34]
              ]
            ],
            type: 'Polygon'
          }
        };
        const polygon1 = {
          type: 'Feature',
          properties: {},
          geometry: {
            coordinates: [
              [
                [122, 34],
                [120, 34],
                [120, 32],
                [122, 32],
                [122, 34]
              ]
            ],
            type: 'Polygon'
          }
        };
        const polygon2 = {
          type: 'Feature',
          properties: {},
          geometry: {
            coordinates: [
              [
                [124, 34],
                [123, 34],
                [123, 32],
                [124, 32],
                [124, 34]
              ]
            ],
            type: 'Polygon'
          }
        };

        searchPolygons = [polygon1, polygon2];
        addLayer('targetPolygon', 'fill', [targetPolygon], {
          'fill-color': 'rgba(0, 0, 255, 0.5)',
          'fill-outline-color': 'purple'
        });
        addLayer('searchPolygons', 'fill', searchPolygons, {
          'fill-color': 'rgba(0, 0, 255, 0.1)',
          'fill-outline-color': 'red'
        });
      }

      function search() {
        clearLayer();
        var relationshipFun = document.getElementById('positon').value;
        var searchResults = [];
        var geometryAnalysis = new maplibregl.supermap.GeometryAnalysis();
        searchPolygons.forEach((polygon) => {
          const booleanResult = geometryAnalysis[relationshipFun](polygon, targetPolygon);
          if (booleanResult) {
            searchResults.push(polygon);
          }
        });
        addLayer('highlightFill', 'fill', searchResults, {
          'fill-color': 'rgba(255, 0, 0, 0.5)',
          'fill-outline-color': 'red'
        });
      }

      function addLayer(id, type, sourceData, paint) {
        map.addLayer({
          id,
          type,
          source: {
            type: 'geojson',
            data: {
              type: 'FeatureCollection',
              features: sourceData
            }
          },
          paint
        });
      }

      function clearLayer() {
        if (!map) {
          return;
        }
        if (map.getLayer('highlightFill')) {
          map.removeLayer('highlightFill');
          map.removeSource('highlightFill');
        }
      }
      initMap(url).then((map) => {
        addFeatures();
      });
    </script>
  </body>
</html>
